/*
 * @Description:
 * @Author:
 * @Date: 2024-10-23 07:09:42
 * @LastEditTime: 2024-10-23 07:20:53
 * @LastEditors: XXX
 */
import "reflect-metadata"
import { ROUTER_MAP, BASE_PATH_MAP } from './constant'

/**
 * @description 生成 http method 装饰器
 * @param method - http method，如 get、post、head
 * @returns Decorator 装饰器
 */
function createMethodDecorator (method: string) {
  // 装饰器接收路由 path 作为参数
  return function httpMethodDecorator (path: string) {
    return (proto: any, name: string) => {
      const target = proto.constructor
      const routeMap = Reflect.getMetadata(ROUTER_MAP, target, 'method') || []
      routeMap.push({ name, method, path })
      Reflect.defineMetadata(ROUTER_MAP, routeMap, target, "method")
    }
  }
}

/**
 * 创建类路径装饰器
 * @param className
 */
function createClassDecorator () {
  // 装饰器接收路由 path 作为参数
  return function httpClassDecorator (basePath: string): ClassDecorator {
    return (proto: any) => {
      const target = proto
      const pathMap = Reflect.getMetadata(BASE_PATH_MAP, target) || []
      pathMap.push({ path: basePath })
      Reflect.defineMetadata(BASE_PATH_MAP, pathMap, target)
    }
  }
}

// 路径前缀
export const prefix = createClassDecorator()

// 导出 http method 装饰器
export const post = createMethodDecorator('post');

export const get = createMethodDecorator('get');

export const del = createMethodDecorator('del');

export const put = createMethodDecorator('put');

export const patch = createMethodDecorator('patch');

export const options = createMethodDecorator('options');

export const head = createMethodDecorator('head');

export const all = createMethodDecorator('all');
